package cn.edu.hbsi.service.impl;

import cn.edu.hbsi.domain.dto.NewsPageDTO;
import cn.edu.hbsi.domain.dto.PublishDTO;
import cn.edu.hbsi.domain.dto.UpdateHeadlineDTO;
import cn.edu.hbsi.domain.result.PageResult;
import cn.edu.hbsi.domain.result.Result;
import cn.edu.hbsi.domain.vo.HeadlineVO;
import cn.edu.hbsi.domain.vo.UpdateHeadlineVO;
import cn.edu.hbsi.utils.JwtHelper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.edu.hbsi.domain.entity.Headline;
import cn.edu.hbsi.service.HeadlineService;
import cn.edu.hbsi.dao.HeadlineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @author 21864
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2025-06-07 18:14:59
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline> implements HeadlineService{

    @Autowired
    private HeadlineMapper headlineMapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 查询新闻分页数据
     * @param newsPageDTO
     * @return
     */
    @Override
    public Result findNewsPage(NewsPageDTO newsPageDTO) {
        // 判断页码和每页记录数是否标准，如果不标准，则设置值
        if (newsPageDTO.getPageNum() == null || newsPageDTO.getPageNum() < 1){
            newsPageDTO.setPageNum(1);
        }
        if (newsPageDTO.getPageSize() == null || newsPageDTO.getPageSize() < 1){
            newsPageDTO.setPageSize(10);
        }

        // 定义条件查询对象
        LambdaQueryWrapper<Headline> wrapper = new LambdaQueryWrapper<Headline>();
        // 分页查询
        Page<Headline> page = new Page<>(newsPageDTO.getPageNum(), newsPageDTO.getPageSize());
        // 定义条件
        wrapper.like(newsPageDTO.getKeyWords() != null,Headline::getTitle, newsPageDTO.getKeyWords())
                .eq(newsPageDTO.getType() != null && newsPageDTO.getType() > 0,Headline::getType, newsPageDTO.getType())
                .eq(Headline::getIsDeleted, 0);
        // 分页查询
        headlineMapper.selectPage(page,wrapper);

        // 判断传入的页码是否大于总页数
        if (newsPageDTO.getPageNum() > page.getPages()){
            // 如果大于，则设置页码为总页数
            newsPageDTO.setPageNum((int) page.getPages());
        }

        // 定义分页对象
        page = new Page<>(newsPageDTO.getPageNum(), newsPageDTO.getPageSize());

        //分页查询
        //查询的结果 "pastHours":"3"   // 发布时间已过小时数 我们查询返回一个map
        //自定义方法
        headlineMapper.selectPageMap(page, newsPageDTO);

        //分页数据封装
        PageResult pageResult = PageResult.builder()
                .pageData(page.getRecords())
                .pageNum((int) page.getCurrent())
                .pageSize((int) page.getSize())
                .totalPage((int) page.getPages())
                .totalSize(page.getTotal())
                .build();

        Map<String,Object> pageInfoMap=new HashMap<>();
        pageInfoMap.put("pageInfo",pageResult);
        // 响应JSON
        return Result.ok(pageInfoMap);
    }

    /**
     * 查询头条详情
     * @param hid
     * @return
     */
    @Override
    public Result showHeadlineDetail(Integer hid) {
        //通过新闻id获取新闻的某些信息
        HeadlineVO headlineVO = headlineMapper.selectHeadlineByHid(hid);

        //浏览量+1
        Headline headline = Headline.builder()
                .hid(headlineVO.getHid())
                .pageViews(headlineVO.getPageViews() + 1)
                .version(headlineVO.getVersion())
                .build();
        headlineMapper.updateById(headline);

        Map map = new HashMap();
        map.put("headline",headlineVO);
        return Result.ok(map);
    }

    /**
     * 新闻发布功能
     * @param token
     * @param publishDTO
     * @return
     */
    @Override
    public Result publish(String token, PublishDTO publishDTO) {
        Long userId = jwtHelper.getUserId(token);
        Headline headline = Headline.builder()
                .title(publishDTO.getTitle())
                .article(publishDTO.getArticle())
                .type(publishDTO.getType())
                .publisher(userId.intValue())
                .pageViews(0)
                .createTime(new Date())
                .updateTime(new Date())
                .version(1)
                .isDeleted(0)
                .build();
        headlineMapper.insert(headline);
        return Result.ok();
    }

    /**
     * 修改新闻回显
     * @param hid
     * @return
     */
    @Override
    public Result findHeadlineByHid(Integer hid) {
        UpdateHeadlineVO updateHeadlineVO = headlineMapper.selectUpdateHeadlineByHid(hid);
        Map map = new HashMap<>();
        map.put("headline",updateHeadlineVO);
        return Result.ok(map);
    }

    /**
     * 修改新闻功能
     * @param updateHeadlineDTO
     * @return
     */
    @Override
    public Result updateHeadline(UpdateHeadlineDTO updateHeadlineDTO) {
        Headline headline = Headline.builder()
                .hid(updateHeadlineDTO.getHid())
                .title(updateHeadlineDTO.getTitle())
                .article(updateHeadlineDTO.getArticle())
                .type(updateHeadlineDTO.getType())
                .updateTime(new Date())
                .version(headlineMapper.selectHeadlineByHid(updateHeadlineDTO.getHid()).getVersion())
                .build();
        headlineMapper.updateById(headline);
        return Result.ok();
    }
}




